package cn.iocoder.yudao.module.shop.staff.controller.app;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.validation.groups.CreateGroup;
import cn.iocoder.yudao.framework.validation.groups.UpdateGroup;
import cn.iocoder.yudao.module.api.shop.staff.dto.ShopStaffSaveDTO;
import cn.iocoder.yudao.module.api.shop.staff.enums.ShopStaffStatusEnum;
import cn.iocoder.yudao.module.api.shop.staff.vo.ShopStaffVO;
import cn.iocoder.yudao.module.shop.staff.pojo.dto.ShopStaffPageDTO;
import cn.iocoder.yudao.module.shop.staff.pojo.dto.ShopStaffStatusSaveDTO;
import cn.iocoder.yudao.module.shop.staff.pojo.po.ShopStaff;
import cn.iocoder.yudao.module.shop.staff.service.ShopStaffService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserLastShopId;

@Tag(name = "用户 App - 店铺员工")
@RestController
@RequestMapping("/shop/staff")
@Validated
public class AppShopStaffController {

    @Resource
    private ShopStaffService shopStaffService;

    @PostMapping("/create")
    @Operation(summary = "创建店员")
    @PreAuthorize("@mm.isMerchant()")
    public CommonResult<Long> createStaff(@Validated(value = {CreateGroup.class}) @RequestBody ShopStaffSaveDTO dto) {
        return success(shopStaffService.createStaff(dto));
    }

    @RequestMapping(value = "/update", method = {RequestMethod.POST, RequestMethod.PUT})
    @Operation(summary = "更新店员")
    @PreAuthorize("@mm.isMerchant()")
    public CommonResult<Boolean> updateStaff(@Validated(value = {UpdateGroup.class}) @RequestBody ShopStaffSaveDTO dto) {
        shopStaffService.updateStaff(dto);
        return success(true);
    }

    @GetMapping("/get-status")
    @Operation(summary = "获得店员状态")
    @PreAuthorize("@mm.isRider()")
    public CommonResult<Integer> getStaffStatus() {
        List<ShopStaff> staffs = shopStaffService.getByMemberUserId(getLoginUserId());
        if (staffs.stream().map(ShopStaff::getStatus).anyMatch(status -> Objects.equals(status, ShopStaffStatusEnum.BUSY.getValue()))) {
            return success(ShopStaffStatusEnum.BUSY.getValue());
        }
        if (CollUtil.isEmpty(staffs) || staffs.stream().map(ShopStaff::getStatus).anyMatch(status -> Objects.equals(status, ShopStaffStatusEnum.REST.getValue()))) {
            return success(ShopStaffStatusEnum.REST.getValue());
        }
        return success(ShopStaffStatusEnum.IDLE.getValue());
    }

    @RequestMapping(value = "/update-status", method = {RequestMethod.POST, RequestMethod.PUT})
    @Operation(summary = "更新店员状态")
    @PreAuthorize("@mm.isRider()")
    public CommonResult<Boolean> updateStaffStatus(@Validated(value = {UpdateGroup.class}) @RequestBody ShopStaffStatusSaveDTO dto) {
        List<ShopStaff> staffs = shopStaffService.getByMemberUserId(getLoginUserId());
        if (CollUtil.isEmpty(staffs)) {
            return success(false);
        }
        if (staffs.stream().map(ShopStaff::getStatus).anyMatch(status -> Objects.equals(status, ShopStaffStatusEnum.BUSY.getValue()))) {
            return success(false);
        }
        int status = Boolean.TRUE.equals(dto.getResting()) ? ShopStaffStatusEnum.REST.getValue() : ShopStaffStatusEnum.IDLE.getValue();
        staffs.forEach(staff -> shopStaffService.updateStaffStatus(staff.getId(), status));
        return success(true);
    }

    @RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.DELETE})
    @Operation(summary = "删除店员")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@mm.isMerchant()")
    public CommonResult<Boolean> deleteStaff(@RequestParam("id") Long id) {
        shopStaffService.deleteStaff(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店员")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@mm.isMerchant()")
    public CommonResult<ShopStaffVO> getStaff(@RequestParam("id") Long id) {
        ShopStaff verifyStaff = shopStaffService.getStaff(id);
        return success(BeanUtils.toBean(verifyStaff, ShopStaffVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得店员列表")
    @PreAuthorize("@mm.isMerchant()")
    public CommonResult<List<ShopStaffVO>> getStaffList() {
        List<ShopStaff> list = shopStaffService.getByShopId(getLoginUserLastShopId(), null);
        List<ShopStaffVO> data = BeanUtils.toBean(list, ShopStaffVO.class);
        return success(data);
    }

    @GetMapping("/page")
    @Operation(summary = "获得店员分页")
    @PreAuthorize("@mm.isMerchant()")
    public CommonResult<PageResult<ShopStaffVO>> getStaffPage(@Valid ShopStaffPageDTO dto) {
        dto.setShopId(getLoginUserLastShopId());
        PageResult<ShopStaff> pageResult = shopStaffService.getStaffPage(dto);
        PageResult<ShopStaffVO> data = BeanUtils.toBean(pageResult, ShopStaffVO.class);
        return success(data);
    }

}